<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : language.manage.php   FILE_PATH : E:\www\\branches\_B2B\system\language.manage.php
 * ....多语言操作
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Tue Jun 24 22:30:11 CST 2008
 */

require_once("../global.inc.php");

class SysLanguageClass extends SystemFrameWork{
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	/**
	 * 多语言对象
	 *
	 * @var obj
	 */
	var $obj_language;
	
	function main(){
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 初始化多语言类
		 */
		if (!is_object($this->obj_language)){
			require_once("language.class.php");
			$this->obj_language = new LanguageClass();
		}
		/**
		 * 语言包
		 */
		$this->getlang("sys_language");
		
		/**
		 * 在页面上显示模板路径
		 */
		$this->output('url',BasePath.'/lang/');
		
		switch($this->_input['action']){
			case "ajax_list":
				$this->_ajax_list();
				break;
			case "add":
				$this->_add();
				break;
			case "save":
				$this->_save();
				break;
			case "modify":
				$this->_modify();
				break;
			case "update":
				$this->_update();
				break;
			case "set_default":
				$this->_set_default();
				break;
			case "del":
				$this->_del();
				break;
			default:
				$this->_list();
		}
		
	}
	/**
	 * 列表页
	 */
	function _list(){
		$this->_ajax_list();
	}
	
	/**
	 * ajax列表页
	 */
	function _ajax_list(){
		$language_array = $this->obj_language->listLanguage($obj_condition,$page);
		/**
		 * 页面输出
		 */
		$this->output('language_array',$language_array);
		$this->showpage('sys_language.list');
	}
	/**
	 * 添加
	 */
	function _add(){
		/**
		 * 页面输出
		 */
		$this->showpage('sys_language.add');
	}
	/**
	 * 录入
	 */
	function _save(){
		/**
		 * 验证提交的数据
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input['txtname'],"require"=>"true","message"=>$this->_lang['errLanguageNameIsEmpty']),
			array("input"=>$this->_input['txtsign'],"require"=>"true","message"=>$this->_lang['errLanguageSignIsEmpty']),
			array("input"=>$this->_input['txtpath'],"require"=>"true","message"=>$this->_lang['errLanguagePathIsEmpty']),
			array("input"=>$this->_input['txtsort'],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errLanguageSortMustBeNumber']),
		);
		$error = $this->obj_validate->validate();
		if ($error != ""){
			$this->redirectPath('error','',$error,1);
		}else {
			//判断语言包目录是否存在
			if (!is_dir('../lang/'.$this->_input['txtpath'])){
				$this->redirectPath('error','',$this->_lang['errLanguageFileIsEmpty'],1);
			}
			//检查该标识是否存在
			$this->checkLanguageSign($this->_input['txtsign']);
			
			$value_array = array();
			$value_array['language_name'] = $this->_input['txtname'];
			$value_array['language_sign'] = $this->_input['txtsign'];
			$value_array['language_path'] = $this->_input['txtpath'];
			$value_array['language_sort'] = $this->_input['txtsort'];
			$value_array['language_state'] = $this->_input['txtstate'];
			
			//如果设置为默认语言，则将其他语言信息更新为非默认语言
			if ($value_array['language_state'] == 1){
				$this->obj_language->updateDefaultState('0');
				$this->setLanguageToConfig($value_array['language_sign']);
			}
			$result = $this->obj_language->addLanguage($value_array);
			if ($result == false){
				$this->redirectPath('error','',$this->_lang['errLanguageAddFaild'],1);
			}else {
				//记录操作日志
				SystemPowerClass::addSysLog($this->_lang['langLanguageLogAdd']);
				$this->redirectPath('error','./language.manage.php?action=list',$this->_lang['langLanguageAddSucc'],1);
			}
		}
	}
	/**
	 * 修改
	 */
	function _modify(){
		/**
		 * 验证提交的数据
		 */
		
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input['id'],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errLanguageIdNotNumber']),
		);
		$error = $this->obj_validate->validate();
		if ($error != ""){
			$this->redirectPath('error','',$error,1);
		}else {
			//取语言信息
			$language_array = $this->obj_language->getOneLanguageById($this->_input['id']);
			/**
			 * 页面输出
			 */
			$this->output("curpage", $this->_input['curpage']);
			$this->output('language_array',$language_array);
			$this->showpage('sys_language.modi');
		}
	}
	
	/**
	 * 保存修改
	 */
	function _update(){
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input['txtname'],"require"=>"true","message"=>$this->_lang['errLanguageNameIsEmpty']),
			array("input"=>$this->_input['txtsign'],"require"=>"true","message"=>$this->_lang['errLanguageSignIsEmpty']),
			array("input"=>$this->_input['txtpath'],"require"=>"true","message"=>$this->_lang['errLanguagePathIsEmpty']),
			array("input"=>$this->_input['txtsort'],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errLanguageSortMustBeNumber']),
		);
		$error = $this->obj_validate->validate();
		if ($error != ""){
			$this->redirectPath('error','',$error,1);
		}else {
			//判断语言包目录是否存在
			if (!is_dir('../lang/'.$this->_input['txtpath'])){
				$this->redirectPath('error','',$this->_lang['errLanguageFileIsEmpty'],1);
			}
			//检查该标识是否存在
			$this->checkLanguageSign($this->_input['txtsign'],$this->_input['language_id']);
			$value_array = array();
			$value_array['language_id'] = $this->_input['language_id'];
			$value_array['language_name'] = $this->_input['txtname'];
			$value_array['language_sign'] = $this->_input['txtsign'];
			$value_array['language_path'] = $this->_input['txtpath'];
			$value_array['language_sort'] = $this->_input['txtsort'];
			$value_array['language_state'] = $this->_input['txtstate'];
			//如果设置为默认语言，则将其他语言信息更新为非默认语言
			if ($value_array['language_state'] == 1){
				$this->obj_language->updateDefaultState(0);
				$this->setLanguageToConfig($value_array['language_sign']);
			}
			$result = $this->obj_language->updateLanguage($value_array);
			if ($result == false){
				$this->redirectPath('error','',$this->_lang['errLanguageAddFaild'],1);
			}else {
				//记录操作日志
				SystemPowerClass::addSysLog($this->_lang['langLanguageLogUpdate']);
				$this->redirectPath('error','./language.manage.php?action=list',$this->_lang['langLanguageUpdateSucc'],1);
			}
		}
	}
	
	/**
	 * 设定默认语言
	 */
	function _set_default(){
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input['id'],"require"=>"true","validator"=>"Number","message"=>$this->_lang['errLanguageIdNotNumber']),
		);
		$error = $this->obj_validate->validate();
		if ($error != ""){
			$this->redirectPath('error','',$error,1);
		}else {
			//更新其他语言为非默认语言
			$this->obj_language->updateDefaultState(0);
			//将该ID的信息设为默认语言
			$value_array = array();
			$value_array['language_id'] = $this->_input['id'];
			$value_array['language_state'] = 1;
			$result = $this->obj_language->updateLanguage($value_array);
			if ($result == false){
				$this->redirectPath('error','',$this->_lang['errLanguageSetFaild'],1);
			}else {
				//写入配置文件
				$language_array = $this->obj_language->getOneLanguageById($this->_input['id']);;
				$this->setLanguageToConfig($language_array['language_sign']);
				//记录操作日志
				SystemPowerClass::addSysLog($this->_lang['langLanguageLogSetDefault']);
				$this->redirectPath('error','',$this->_lang['langLanguageSetSucc'],1);
			}
		}
	}
	
	/**
	 * 删除
	 */
	function _del(){
		if (is_array($this->_input['language_id'])){
			foreach ($this->_input['language_id'] as $k => $v){
				$this->obj_language->delLanguage($v);
			}
			//检查是否还存在默认语言，如果没有，则默认第一个信息为默认语言
			$obj_condition['state'] = 1;
			$language_array = $this->obj_language->listLanguage($obj_condition,$page);
			if (empty($language_array)){
				unset($obj_condition);
				$language_array = $this->obj_language->listLanguage($obj_condition,$page);
				if (!empty($language_array)){//更新第一个为默认语言
					$value_array = array();
					$value_array['language_id'] = $language_array[0]['language_id'];
					$value_array['language_state'] = 1;
					$result = $this->obj_language->updateLanguage($value_array);
				}
			}
			//记录操作日志
			SystemPowerClass::addSysLog($this->_lang['langLanguageLogDel']);
			$this->redirectPath('error','',$this->_lang['langLanguageDelSucc'],1);
		}else {
			$this->redirectPath('error','',$this->_lang['errLanguageDelFaild'],1);
		}
	}
	
	/**
	 * 检查语言标识是否存在
	 */
	function checkLanguageSign($language_sign,$language_id=''){
		$obj_condition['sign'] = $language_sign;
		if ($language_id != ''){
			$obj_condition['no_id'] = $language_id;
		}
		$language_array = $this->obj_language->listLanguage($obj_condition,$page);
		if (!empty($language_array)){
			$this->redirectPath('error','',$this->_lang['errLanguageSignIsDuplicate'],1);
		}else {
			return true;
		}
	}
	
	/**
	 * 将默认语言写入配置文件
	 */
	function setLanguageToConfig($language){
		if ($language != ''){
			require_once("inifileoperate.class.php");
			$obj_config = new IniFileOperate(BasePath . "/config/config.ini.php");
			$obj_config->setIniFile("websit","versionarea",$language);
			unset($obj_config);
			return false;
		}
		return false;
	}
}

$language = new SysLanguageClass();
$language->main();
unset($language);
?>
